﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogicLayer
{
    public   class EmployeeFacade
    {
        InventorySoreDataContext context;
        Employee employee;
        public EmployeeFacade()
        {
            context = new InventorySoreDataContext();
        }
        public IQueryable FindAll()
        {
            var employees = from emp in context.Employees select emp;
            return employees;
        }

        public Employee FindEmployeeByName(string empName)
        {
            var em = (from emp in context.Employees where emp.EmployeeName == empName select emp).Single();
            return em;
        }

        public Employee FindEmployeeByID(string EmployeeID)
        { 
            var employeeres =( from emp in context.Employees where emp.EmployeeID == EmployeeID select emp).SingleOrDefault();
            employee = employeeres;
            return employee;
        }
        public string FindEmail(string DeptCode, string RoleName)
        {
            var email = (from emp in context.Employees where emp.Departmentcode == DeptCode && emp.EmployeeRole.Role == RoleName select emp).SingleOrDefault();
            return email.Email.ToString();
                       
         }

        public string getRoleID(string RoleName)
        {
            var roles = (from role in context.EmployeeRoles where role.Role == RoleName select role).Single();
            return roles.RoleId;
            
        }
        public IQueryable FindEmployeeByDepartment(string DepartCode)
        { 
            var employees=from emp in context.Employees where emp.Departmentcode==DepartCode select emp;
            return employees;
        }

        public void UpdateRole(string EmployeeID, string NewRole)
        {
            employee = FindEmployeeByID(EmployeeID);
            string roleID = getRoleID(NewRole);
            employee.RoleId = roleID;
            employee.EmployeeRole.RoleId = roleID;
            context.SubmitChanges();
        }

        public void insertemployee(Employee e)
        {
            context.Employees.InsertOnSubmit(e);
            context.SubmitChanges();
        }

        //public static void Main(string[] args)
        //{
        //    var result = new EmployeeFacade().FindAll();
        //    foreach (Employee e in result)
        //    {
        //        Console.WriteLine(e.EmployeeID + " " + e.EmployeeRole.Role);
        //    }
        //    Console.ReadLine();
        //    new EmployeeFacade().UpdateRole("A0102959Y", "Department Head");
        //    Console.WriteLine("\n\n\nnew record\n\n");
        //    Console.ReadLine();

        //    ////StockCard stc = new ItemFacade().FindStockCardByItemCode("C006");
            //Console.WriteLine(stc.StockCardID);
            //Console.WriteLine(stc.ItemCode);
            //Console.WriteLine(stc.Description);
            //Console.WriteLine(stc.Date);
            //Console.WriteLine(stc.Operation + stc.Balance);

        }


        //public static void Main(string []args)
        //{
        //    var result = new EmployeeFacade().FindAll();
        //    foreach (Employee e in result)
        //    {
        //        Console.WriteLine(e.EmployeeID + " " + e.EmployeeRole.Role);
        //    }
        //    Console.ReadLine();
        //    new EmployeeFacade().UpdateRole("A0102959Y", "Department Head");
        //    Console.WriteLine("\n\n\nnew record\n\n");     
        //    Console.ReadLine();
        // }
}
    
